package com.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.entity.Goods;
import com.entity.Plan;
import com.entity.Purchase;
import com.entity.PurchaseBill;
import com.entity.PurchaseDetail;
import com.entity.Suppiler;
import com.service.GoodsService;
import com.service.PlanService;
import com.service.PurchaseBillService;
import com.service.PurchaseDetailService;
import com.service.PurchaseService;
import com.service.SuppilerService;

@Controller
@RequestMapping("/caigou")
public class PlanController {
	Map<String, String> map=new HashMap<String, String>();
	@Autowired
	PlanService planService;
	@Autowired
	PurchaseDetailService purchaseDetailService;
	@Autowired
	SuppilerService suppilerService;
	@Autowired
	GoodsService goodsService;
	@Autowired
	PurchaseService purchaseService;
	@Autowired
	PurchaseBillService purchaseBillService;
	@RequestMapping("/list")
	public String planlist(Model model){
		List<Plan>planlist = planService.selectAll();
		model.addAttribute("planlist", planlist);
		return "developer/appinfolist";
	}
	@RequestMapping("/add")
	public String add(Model model){
		List<Suppiler>suppiler=suppilerService.selectAll();
		model.addAttribute("suppiler", suppiler);
		return "developer/appinfoadd";
	}
	@RequestMapping("/insert")
	public String insert(Plan plan){
		System.out.println(plan.getPlanHandled());
		Suppiler suppiler123= suppilerService.selectByPrimaryKey(Integer.parseInt(plan.getPlanHandled()));
		System.out.println(suppiler123.getSupplierContacts());
		plan.setPlanHandled(suppiler123.getSupplierName());
		int x = planService.insert(plan);
		if(x!=0){
			return "redirect:list";
		}
		return "developer/appinfoadd";
	}
	@RequestMapping("/appinfomodify")
	public String modify(int id,Model model){
		Plan plan = planService.selectByPrimaryKey(id);
		model.addAttribute("plan", plan);
		List<Suppiler>suppiler=suppilerService.selectAll();
		model.addAttribute("suppiler", suppiler);
		return "developer/appinfomodify";
	}
	@RequestMapping("/upd")
	public String upd(Plan plan){
		int x = planService.updateByPrimaryKey(plan);
		if(x!=0){
			return "redirect:list";
		}
		return "developer/appinfomodify";
	}
	@ResponseBody
	@RequestMapping("/del")
	public Map<String, String> del(@RequestParam(value = "id", required = false, defaultValue = "0") int id){
		int x=planService.deleteByPrimaryKey(id);
		if(x!=0){
				map.put("delResult", "true");
			}else {
				map.put("delResult", "false");
		}
		return map;
	}
	@RequestMapping("/appversionadd")
	public String app(int id,Model model){
		List<PurchaseDetail> purchaseDetail = purchaseDetailService.selectByPrimaryKey(id);
		List<Suppiler>suppiler=suppilerService.selectAll();
		List<Goods>goods=goodsService.selectAll();
		model.addAttribute("purchaseDetail", purchaseDetail);
		model.addAttribute("suppiler", suppiler);
		model.addAttribute("goods", goods);
		model.addAttribute("id",id);
		return "developer/appversionadd";
	}
	@RequestMapping("/addversionsave")
	public String addversionsave(PurchaseDetail purchaseDetail){
		int id =purchaseDetail.getPlanId();
		Goods goods=goodsService.selectByPrimaryKey(Integer.parseInt(purchaseDetail.getGoodsname()));
		Suppiler suppoler=suppilerService.selectByPrimaryKey(Integer.parseInt(purchaseDetail.getDepartment()));
		purchaseDetail.setDepartment(suppoler.getSupplierName());
		purchaseDetail.setGoodsname(goods.getgName());
		purchaseDetail.setExecute(1);
		purchaseDetail.setNoquantity(purchaseDetail.getQuantity());
		int x = purchaseDetailService.insert(purchaseDetail);
		if(x!=0){
			int planPrice=Integer.parseInt(purchaseDetail.getRemark());
			planService.updprice(id);
			return "redirect:appversionadd?id="+id;
		}
		return "developer/appversionadd?ud="+id;
	}
	@ResponseBody
	@RequestMapping("/appverion")
	public Goods  appverion(@RequestParam(value = "id", required = false, defaultValue = "0") int id){
		Goods good = goodsService.selectByPrimaryKey(id);
		return good;
	}
	@ResponseBody
	@RequestMapping("/updataAudit")
	public  Map<String, String> updA(@RequestParam(value = "planId", required = false, defaultValue = "0")int planId,@RequestParam(value = "planAudit", required = false, defaultValue = "0")int planAudit){
		int x=planService.updAudit( planId,planAudit);
		if(x>0){
			map.put("delResult", "true");
		}else{
			map.put("delResult", "false");
		}
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/updataExecute")
	public  Map<String, String> updE(@RequestParam(value = "planId", required = false, defaultValue = "0")int planId,@RequestParam(value = "planExecute", required = false, defaultValue = "0")int planExecute){
		int x=planService.updExecute( planId,planExecute);
		if(x>0){
		Purchase purchase =new Purchase();
		Plan plan=	planService.selectByPrimaryKey(planId);
		List<PurchaseDetail> purchaseDetail = purchaseDetailService.selectByPrimaryKey(planId);
		purchase.setPurchaseId(plan.getPlanId());
		purchase.setPlanId(plan.getPlanId());
		purchase.setSuppilername(plan.getPlanHandled());
		purchase.setPurchaseData(plan.getPlanData());
		purchase.setPurchaseHandled(plan.getPlanHandled());
		purchase.setPurchasePrice(plan.getPlanPrice()); 
		purchase.setExecute(1);
		int z= purchaseService.insert(purchase);
		if(z>0){
			for (PurchaseDetail purchaseDetail2 : purchaseDetail) {
				PurchaseBill purchasebill =new PurchaseBill();
				purchasebill.setGoodId(purchaseDetail2.getDetailId());
				Goods goods=goodsService.selectAlls(purchaseDetail2.getGoodsname());
				purchasebill.setGoodCount(goods.getGid());
				purchasebill.setPrice(Integer.parseInt(purchaseDetail2.getRemark()));
				purchasebill.setYetCount(purchaseDetail2.getNoquantity());
				purchasebill.setNotCount(0);
				purchasebill.setPlanId(purchaseDetail2.getPlanId());
				purchasebill.setExecute(1);
				purchaseBillService.insert(purchasebill);
			}
		}
			map.put("delResult", "true");
		}else{
			map.put("delResult", "false");
		}
		return map;
	}
	@RequestMapping("/ruku")
	public String ruku(int id,Model model){
		List<PurchaseBill> purchasebill =purchaseBillService.selectByPrimaryKey(id);
		Purchase purchase =purchaseService.selectByPrimaryKey(id);
		model.addAttribute("purchasebill", purchasebill);
		model.addAttribute("purchase", purchase);
		return "developer/appinfoview";
	}
	@RequestMapping("/deldetail")
	public String del(int id,Model model){
		PurchaseDetail purchaseDetail = purchaseDetailService.selectByPrimaryKeys(id);
		int x = purchaseDetailService.deleteByPrimaryKey(id);
		if(x!=0){
			return "redirect:appversionadd?id="+purchaseDetail.getPlanId();
		}
		return "developer:appversionadd"+purchaseDetail.getPlanId();
	}

}
